package day_2022_9_to_12.Day;

import java.util.*;

/**
 * @author haomin
 * @date 2022/09/05 09:30
 **/
public class Day3_2_Offer50_FirstUniqChar {
    class Solution1 {
        // map
        public char firstUniqChar(String s) {
            Map<Character, Integer> map = new HashMap<>();
            for(int i = 0; i < s.length(); ++i){
                char c = s.charAt(i);
                map.put(c, map.getOrDefault(c, 0)+1);
            }
            for(int i = 0; i < s.length(); ++i){
                char c = s.charAt(i);
                if(map.get(c) == 1) return c;
            }
            return ' ';
        }
    }
    class Solution {
        // 桶计数
        public char firstUniqChar(String s) {
            int[] arr = new int[26];
            for(int i = 0; i < s.length();++i){
                arr[s.charAt(i)-'a']++;
            }
            for(int i = 0; i < s.length();++i){
                if(arr[s.charAt(i)-'a'] == 1) return s.charAt(i);
            }
            return ' ';
        }
    }
}